package freemind.controller;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;

import freemind.controller.NodeAdapterBase;;

public class TheMap implements Serializable{
	private static final long serialVersionUID = 1L;
	
	private NodeAdapterBase root;
	private ArrayList<NodeAdapterBase> criados,salvos;
	private ArrayList<Integer> cenas,dialogos,copias;
	
	private int protocolo;
	
	public transient static int REQUEST_LOAD = 0;
	public transient static int REQUEST_SAVE = 1;
	public transient static int RESPONSE_LOAD = 2;
	public transient static int RESPONSE_SAVE= 3;
	public transient static int REQUEST_REMOVE = 4;
	public transient static int RESPONSE_REMOVE = 5;
	
	public TheMap(){
		this.criados = new ArrayList<NodeAdapterBase>();
		this.salvos = new ArrayList<NodeAdapterBase>();
	}
	
	public TheMap(NodeAdapterBase root){
		this();
		this.root = root;
	}
	
	public NodeAdapterBase getRoot(){
		return this.root;
	}
	
	public void setRoot(NodeAdapterBase root){
		this.root = root;
	}
	
	public void setProtocolo(int protocolo){
		this.protocolo = protocolo;		
	}
	
	public int getProtocolo(){
		return this.protocolo;
	}
	
	public void setCriados(ArrayList<NodeAdapterBase> criados){
		this.criados = criados;
	}
	
	public void setSalvos(ArrayList<NodeAdapterBase> salvos){
		this.salvos = salvos;
	}
	
	public void addCriados(NodeAdapterBase node){
		this.criados.add(node);
	}
	
	public void addSalvos(NodeAdapterBase node){
		this.salvos.add(node);
	}
	
	public ArrayList<NodeAdapterBase> getCriados(){
		return this.criados;
	}

	public ArrayList<NodeAdapterBase> getSalvos(){
		return this.salvos;
	}
	
	public void addCenasRemover(Integer id){
		this.cenas.add(id);
	}
	
	public void addDialogosRemover(Integer id){
		this.dialogos.add(id);
	}
	
	public void addCopiasRemover(Integer id){
		this.copias.add(id);
	}	
	
	public ArrayList<Integer> getCenasRemover(){
		return this.cenas;
	}
	
	public ArrayList<Integer> getDialogosRemover(){
		return this.cenas;
	}
	
	public ArrayList<Integer> getCopiasRemover(){
		return this.cenas;
	}	
	
	public static TheMap readMap(String protocol){
		String[] b_str = protocol.split(",");
		byte[] bytes = new byte[b_str.length];
		int i = 0;
		
		for(String a:b_str){
			bytes[i] = ( Integer.valueOf(a)).byteValue();
			i++;
		}
		
		try {
			ByteArrayInputStream bais = new ByteArrayInputStream( bytes );
			ObjectInputStream ois = new ObjectInputStream(bais);
			
			TheMap map = (TheMap) ois.readObject();
			ois.close();
			
			return map;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
	
	public static byte[] writeMap(Object map){
		
		try {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();							
			ObjectOutputStream oos = new ObjectOutputStream( baos );
			
			oos.writeObject(map);
			
			oos.flush();
			oos.close();
	
			return baos.toByteArray();
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
							
		return new byte[10];
	}		
	
	public static FileOutputStream writeMapInAFile(Object map,String path){
		
		try {
			FileOutputStream fos = new FileOutputStream(path);							
			ObjectOutputStream oos = new ObjectOutputStream( fos );
			
			oos.writeObject(map);
			
			oos.flush();
			oos.close();
	
			return fos;
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
							
		return null;
	}		

	public static String createMapArrayCallJS(byte[] byteMap){		
		String js = "";
		
		for(int i=0;i<byteMap.length;i++){
			js += byteMap[i] + ",";
		}
		
		js = js.substring(0, js.length()-1);
		
		return js;
	}	
	
}
